#include <stdio.h>
#include <time.h>
#include<unistd.h>

int row(){
    FILE*fp_n = fopen("./test.txt","r");
    if(fp_n == NULL){
        printf("fopen err\n");
        return -1;
    }

    int n=0;

    // char chs[32]={0};
    // int len=sizeof(chs)/sizeof(chs[0]);
    // while(1){
        
    //     if(fgets(chs,len,fp_n) == NULL){
    //         n++;
    //         break;
    //     }

    //     if(chs[(strlen(chs))-1] == '\n'){
    //         n++;
    //     }
    // }

    char c=0;
    while (1){
        if((c = fgetc (fp_n)) == -1){
            n++;
            break;
        }
	    if (c == '\n'){
            n++;
        }
			
	}

    fclose(fp_n);
    fp_n=NULL;
    return n;
}

int main()
{
    FILE*fp= fopen("./test.txt","a+");
    if(fp == NULL){
        printf("fopen err\n");
        return -1;
    }
    time_t timep;
    struct tm *p;

    time(&timep); //获取从1970至今过了多少秒，存入time_t类型的timep
    p = localtime(&timep);//用localtime将秒数转化为struct tm结构体

    
    int xuhao=row();
    while(1){
		time (&timep);
		p = localtime (&timep);
		fprintf(fp,"%d,%d-%d-%d %d:%d:%d\n",xuhao++,1900 + p->tm_year, 1+ p->tm_mon, p->tm_mday,p->tm_hour, p->tm_min, p->tm_sec);
		fflush (fp);
		sleep(1);
    }


    return 0;
}
